<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: Mr. Xu
 * @Date: 2020-12-09 15:36:19
 * @LastEditors: Mr. Xu
 * @LastEditTime: 2020-12-15 09:49:19
 */

namespace app\admin\controller;

use think\Controller;

class Base extends Controller{
		public $handle_url;  //当前url
		public $admin_info;  //当前url
		public $company=[];  //定义当前公司数组
		public $menus=[];  //定义当前公司数组
		//过滤限制权限
		public $filter = [];
		
		
	    public function initialize() {
			parent::initialize();
			if(session("filter") && count(session("filter"))==count($this->filter)){
				$this->filter = session("filter");
			}else{
				foreach($this->filter as $k=>$v){
					$this->filter[$k] = strtolower($v);
				}
				session('filter',$this->filter);
			}
			
			$this->handle_url = $this->request->module() . '/' . $this->request->controller() . '/' . $this->request->action();
            //权限检测
            $this->checkAuth();
	      	
    	}

    /**
     * 权限检查
     * @return bool
     */
    public function checkAuth() {
		
		if(!session('central_admin')){
			$url=url('admin/login/index');
			echo "<script>parent.location.href='{$url}';</script>";die();
		}
		$this->menus=$this->auth_menus();
		//过滤超级管理员
		if(!in_array(strtolower($this->handle_url),$this->auth_menus()) && session("central_cate_id")!=1){
			var_dump(strtolower($this->handle_url));
			$this->error('您没有权限访问！！！');
		}
		
	}
	
	
	
	
	/**
     * @Function: cache_menu  得到当前角色的权限
     * @Author: 蔡孟光
     * @LastEditors: 蔡孟光
     * @Date: 2020-04-09 11:00:55
     * @LastEditTime: 2020-04-09 17:56:57
     * @param {type} 
     * @return: 
     */
	public function auth_menus(){
		$power = db('admin_cate')->where(['id'=>session("central_cate_id")])->find();//得到该用户角色
		$menu = db('admin_menu')->where(['types'=>1,'id'=>['in',$power['permissions']]])->field('module,controller,function')->select();//得到该角色拥有权限
		//添加url
		$menus=[];        
        foreach ($menu as $key => $value) {
			$url = strtolower($value['module'].'/'.$value['controller'].'/'.$value['function']);
			if($url!="//"){
				$menus[]= $url;
			}
		}
		$menus_data=array_merge($menus,$this->filter);
		return $menus_data;
	}
	
	
	
}



?>